package login.validate;


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import register.students.EmailStore;

/**
 * Servlet implementation class Validate
 */
public class Validate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Validate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String uname = request.getParameter("username");
		String pwd = request.getParameter("password");
		
		//String user = check(uname);
		EmailStore e = new EmailStore();
		boolean user = e.validate(uname);
		
		if(! user)
		{
			boolean auth = authenticate(uname, pwd);
			if(auth)
			{
				response.sendRedirect("./welcome.html");
			}
			else
			{
				System.out.println("Password is wrong");
				response.sendRedirect("./index.html");
			}
		}
		else
		{
			System.out.println("username does not exists");
		}
		
	}

	private boolean authenticate(String user, String pwd) {
		
		//String query = "Select * from users where useremail='"+ usremail.toLowerCase() +"'";
		String query = "Select * from users where useremail='"+ user.toLowerCase() +"'";
	    String connectionString = "jdbc:mysql://localhost:3306/yooim";
	    String userName = "root";
	    String password = "mapat";
	    Connection conn = null; 
	    ResultSet rs = null;
	    try {
	    	
	    	Class.forName("com.mysql.jdbc.Driver").newInstance();
	    	conn = DriverManager.getConnection(connectionString,userName,password);
	    	Statement st = conn.createStatement();
	    	rs = st.executeQuery(query);
	    	System.out.println(rs.getFetchSize());
	    	//conn.close();
	    }
	    catch(Exception e)
	    {
	    	System.out.println("Error while connection to DB - validate : "+e.toString());
	    }
	    
	    	try {
	    		
				if(rs.first() && (rs.getString(3)).equalsIgnoreCase(pwd))
				{
					System.out.println("Good Pwd and Username");
					conn.close();
					return true;
				}
					else {
						System.out.println("wrong password");
						conn.close();
						return false;
					}
	    		
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("error SQL"+e.toString());
				//conn.close();
				return false;
			}
	}

	private String check(String uname) {
		
		
		return uname;
	}



	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
